package com.ecube.test.control;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.StorageFunctionEnum;
import com.ecube.protocol.storage.StoragePowerAdjustSetDischargeResp;
import com.ecube.protocol.storage.StoragePowerAdjustSetResp;
import com.ecube.utils.EnumUtils;
import lombok.Builder;
import lombok.Data;


/**
 * 储能设备
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/5 19:36
 */
public class SendStorageControl {

    public static void main(String[] args) {

        sendAdjustSetResp("FAT", "100100001", "101202509", StorageInfo.builder().index(173).effect(2).result(2).reason(0).build());
        //sendAdjustSetResp("FAT", "100100001", "101202508", StorageInfo.builder().index(61).result(1).build());

        //sendAdjustSetDischargeResp("FAT", "100100001", "101202508", StorageInfo.builder().effect(2).result(1).index(138).build());
        //sendAdjustSetDischargeResp("FAT", "100100001", "101202509", StorageInfo.builder().effect(2).result(1).index(137).build());
    }


    /**
     * 放电-功率调节设置回复
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     * @param info info
     */
    private static void sendAdjustSetDischargeResp(String env, String productCode, String deviceCode, StorageInfo info) {
        StorageFunctionEnum function = (StorageFunctionEnum) EnumUtils.getFunction(StoragePowerAdjustSetDischargeResp.class);
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        StoragePowerAdjustSetDischargeResp adjustSetResp = new StoragePowerAdjustSetDischargeResp();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V1.0.1");
        reportHand.setIndex(info.getIndex());
        reportStandard.setHeader(reportHand);

        // 设置储能信息
        adjustSetResp.setEffect(info.getEffect());
        adjustSetResp.setPcsNo(info.getPcsNo());
        adjustSetResp.setResult(info.getResult());
        adjustSetResp.setReason(info.getReason());
        reportStandard.setDataBody(adjustSetResp);

        String reportContent = JSON.toJSONString(reportStandard);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }
    /**
     * 充电-功率调节设置回复
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     * @param info info
     */
    private static void sendAdjustSetResp(String env, String productCode, String deviceCode, StorageInfo info) {
        StorageFunctionEnum function = (StorageFunctionEnum) EnumUtils.getFunction(StoragePowerAdjustSetResp.class);
        if(function == null){
            return;
        }

        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        StoragePowerAdjustSetResp adjustSetResp = new StoragePowerAdjustSetResp();

        DataReportStandard reportStandard = new DataReportStandard();
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V1.0.1");
        reportHand.setIndex(info.getIndex());
        reportStandard.setHeader(reportHand);

        // 设置储能信息
        adjustSetResp.setEffect(info.getEffect());
        adjustSetResp.setPcsNo(info.getPcsNo());
        adjustSetResp.setResult(info.getResult());
        adjustSetResp.setReason(info.getReason());
        reportStandard.setDataBody(adjustSetResp);

        String reportContent = JSON.toJSONString(reportStandard);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }

    @Data
    @Builder
    private static class StorageInfo {

        /**
         * PCS序号
         */
        private String pcsNo = "0";

        /**
         * 结果：1-成功；2-失败
         */
        private Integer result = 1;

        /**
         * 生效类型：1-立即生效；2-取消生效；
         */
        private Integer effect;

        /**
         * 失败原因: 0-功能不可用；1-参数错误；
         */
        private Integer reason;

        /**
         * 序号
         */
        private Integer index;

    }
}
